﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class HRMKeyPerformanceAreaList_POP : System.Web.UI.Page
    {
        private HRMKeyPerformanceAreaListBLL cmd = new HRMKeyPerformanceAreaListBLL();
        private SsMng creatby = new SsMng();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int selectKeyArea = Convert.ToInt32(Request.QueryString["KeyArea"]);
                int selectPosSeq = Convert.ToInt32(Request.QueryString["PosSeq"]);
                int selectKPASeq = Convert.ToInt32(Request.QueryString["KPASeq"]);
                string MODE = Request.QueryString["Mode"];
                
                LoadKeyArea(selectKeyArea, selectKPASeq, selectPosSeq, MODE);
            }
        }

        private void LoadKeyArea(int selectKeyArea, int selectKPASeq, int selectPosSeq, string MODE)
        {
            if ( MODE == "Edit" )
            {
                DataTable dt = cmd.ListKeyArea();
                cboRadKeyArea.DataSource = dt;
                cboRadKeyArea.DataBind();
                cboRadKeyArea.SelectedValue = selectKeyArea.ToString();

                DataTable dt1 = cmd.LoadKeyAreaDetail(selectKPASeq);
                object KPAdiscription = dt1.Rows[0][1];
                txtRadKeyDesc.Text = KPAdiscription == DBNull.Value ? "" : KPAdiscription.ToString();

                object KPAweight = dt1.Rows[0][2];
                txtRadKeyWeight.Text = KPAweight == DBNull.Value ? "" : KPAweight.ToString();
            }
            else if ( MODE == "AddNew" )
            {
                DataTable dt = cmd.ListKeyArea();
                cboRadKeyArea.DataSource = dt;
                cboRadKeyArea.DataBind();
                cboRadKeyArea.Items.Insert(0, new RadComboBoxItem("----- เลือกขอบเขตความรับผิดชอบหลัก -----", ""));
            }
        }

        private bool OnValidationWeight(int intPosSeq, int? intWeight, int intSelectKPASeq)
        {
            DataTable dt = cmd.LoadGridKeyArea(intPosSeq);
            int intKPATotalWeight = 0;
            int intKPACurrentWeight = 0;
            int intTotal = 0;
            int intResultWeight = 0;
            int intLimit = 100;

            if (dt.Rows.Count > 0)
            {
                intKPATotalWeight = Convert.ToInt32(dt.Compute("sum(weight)", ""));
                if (dt.Select(string.Format("kpa_seq = {0}", intSelectKPASeq)).Length > 0)
                {
                    DataRow[] drow =  dt.Select(string.Format("kpa_seq = {0}", intSelectKPASeq));
                    object KPAweight = drow[0][4];
                    intKPACurrentWeight = Convert.ToInt32(KPAweight);
                }               
              
            }

            //int intTotalWeight = intKPATotalWeight + Convert.ToInt32(intWeight);
            //int intOver = intTotalWeight - intLimit;
            //int ans = intOver - (Convert.ToInt32(intWeight) - intKPAWeight);

            intResultWeight = intKPATotalWeight - intKPACurrentWeight;
            intTotal = intResultWeight + Convert.ToInt32(intWeight);

            if ( intWeight == null )
            {
                main.MessageAlert( Page, "กรุณากรอกข้อมูลน้ำหนัก" );
                return false;
            }

            if (intLimit - intTotal < 0)
            {
                main.MessageAlert(Page, "น้ำหนักรวมไม่เกิน 100 " + "   รวมน้ำหนักที่กรอก : " + intTotal);
                return false;
            }

            //if ( intTotalWeight > 100 )
            //{
            //    if (Convert.ToInt32(intWeight) > ans)
            //    {
            //        main.MessageAlert(Page, "น้ำหนักรวมไม่เกิน 100 " + "   น้ำหนักที่กรอกเกิน : " + intOver);
            //        return false;
            //    }
            //    //else if (Convert.ToInt32(intWeight) <= ans)
            //    //{
            //    //    main.MessageAlert(Page, "xxxx ");
            //    //    return false;
            //    //}
                
            //    main.MessageAlert(Page, "น้ำหนักรวมไม่เกิน 100 " + "   ppppp : " + intOver );
            //    return false;
            //}

            

            return true;
        }

        private bool OnValidationKeyArea(int intPosSeq,string strKeyArea)
        {
            DataTable dt = cmd.LoadGridKeyArea(intPosSeq);

            if (strKeyArea == "")
            {
                main.MessageAlert(Page, "กรุณาเลือกข้อมูลขอบเขตความรับผิดชอบหลัก");
                return false;
            }

            if (dt.Select(string.Format("key_area_seq = {0}", strKeyArea)).Length > 0)
            {
                main.MessageAlert(Page, "ข้อมูลขอบเขตความรับผิดชอบหลักมีอยู่แล้วในระบบ");
                return false;
            }

            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int intSelectKeyArea = Convert.ToInt32(Request.QueryString["KeyArea"]);
            int intSelectPosSeq = Convert.ToInt32(Request.QueryString["PosSeq"]);
            int intSelectKPASeq = Convert.ToInt32(Request.QueryString["KPASeq"]);
            string MODE = Request.QueryString["Mode"];

            if (MODE == "Edit")
            {
                string strSelectKeyArea = cboRadKeyArea.SelectedValue;
                string strDescription = txtRadKeyDesc.Text;
                Nullable<int> intWeight = string.IsNullOrEmpty(txtRadKeyWeight.Text) ? (int?)null : Int32.Parse(txtRadKeyWeight.Text);

                if (strSelectKeyArea == intSelectKeyArea.ToString())
                {
                    if (OnValidationWeight(intSelectPosSeq, intWeight, intSelectKPASeq))
                    {
                        cmd.UpdateKeyArea(intSelectKPASeq, intSelectKeyArea, strDescription, intWeight, creatby.EmpSeq);
                        Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                    }
                }
                else if (OnValidationKeyArea(intSelectPosSeq, strSelectKeyArea))
                {
                    if (OnValidationWeight(intSelectPosSeq, intWeight, intSelectKPASeq))
                    {
                        int intKeyArea = Convert.ToInt32(strSelectKeyArea);
                        cmd.UpdateKeyArea(intSelectKPASeq, intKeyArea, strDescription, intWeight, creatby.EmpSeq);
                        Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                    }
                }
                cboRadKeyArea.SelectedValue = intSelectKeyArea.ToString();
            }
            else if (MODE == "AddNew")
            {
                string strSelectKeyArea = cboRadKeyArea.SelectedValue;
                string description = txtRadKeyDesc.Text;
                Nullable<int> intWeight = string.IsNullOrEmpty(txtRadKeyWeight.Text) ? (int?)null : Int32.Parse(txtRadKeyWeight.Text);
                              
                if (OnValidationKeyArea(intSelectPosSeq, strSelectKeyArea))
                {
                    if (OnValidationWeight(intSelectPosSeq, intWeight, intSelectKPASeq))
                    {
                        int intKeyArea = Convert.ToInt32(strSelectKeyArea);
                        cmd.InsertKeyArea(intSelectPosSeq, intKeyArea, description, intWeight, creatby.EmpSeq);
                        Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                    }
                }
                
                
            }
        }


    }
}